﻿using System;
using System.Collections;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using BITS.PreCompiler.CSharp.Visitors;
using Roslyn.Compilers.CSharp;

namespace BITS.PreCompiler.CSharp.Syntax
{
    public interface IBaseComment
    {
        SyntaxTriviaList GetSyntaxNode();
    }

    public class Comment : IBaseComment, IVisitable<Comment>
        , IEnumerable<String>
        , IEnumerable<IEnumerable<String>>
    {
        # region • Fields & Properties •

        /// <summary>
        /// Holds the comment text.
        /// </summary>
        private readonly StringBuilder text = new StringBuilder();

        /// <summary>
        /// Holds the comment text.
        /// </summary>
        public StringBuilder Text
        {
            get { return this.text; }
        }

        public virtual SyntaxTriviaList SyntaxTriviaList
        {
            get
            {
                return Roslyn.Compilers.CSharp.Syntax
                    .ParseLeadingTrivia(this.Text.ToString());
            }
        }

        # endregion Fields & Properties

        # region • Methods •

        /// <summary>
        /// Gets the syntax triva.
        /// </summary>
        /// <returns></returns>
        public virtual SyntaxTriviaList GetSyntaxNode()
        {
            return this.SyntaxTriviaList;
        }

        /// <summary>
        /// Gets the syntax triva.
        /// </summary>
        /// <returns></returns>
        SyntaxTriviaList IBaseComment.GetSyntaxNode()
        {
            return this.GetSyntaxNode();
        }

        /// <summary>
        /// Visitor pattern.
        /// </summary>
        /// <param name="visitor"></param>
        /// <returns></returns>
        public virtual Comment Accept(ISyntaxVisitor<Comment> visitor)
        {
            return visitor.Visit(this);
        }

        /// <summary>
        /// Adds the specified text.
        /// </summary>
        /// <param name="line">The text.</param>
        public virtual Comment Add(String line)
        {
            return this.AppendLine(line);
        }

        /// <summary>
        /// Adds the specified text lines.
        /// </summary>
        /// <param name="textLines">The text lines.</param>
        public virtual Comment Add(IEnumerable<String> textLines)
        {
            foreach (var line in textLines)
                this.Add(line);

            return this;
        }

        /// <summary>
        /// Adds the specified text.
        /// </summary>
        /// <param name="line">The text.</param>
        /// <param name="params">The params for text.</param>
        public virtual Comment Append(String line, params Object[] @params)
        {
            if (String.IsNullOrEmpty(line))
                return this;

            this.Text.AppendFormat(line, @params);

            return this;
        }

        /// <summary>
        /// Adds the specified text.
        /// </summary>
        /// <param name="line">The text.</param>
        /// <param name="params">The params for text.</param>
        public virtual Comment AppendLine(String line, params Object[] @params)
        {
            if (String.IsNullOrEmpty(line))
                return this;

            this.Text.AppendFormat(line, @params);
            this.Text.AppendLine();

            return this;
        }

        /// <summary>
        /// Returns a <see cref="string"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="string"/> that represents this instance.
        /// </returns>
        public override String ToString()
        {
            var result = new StringBuilder();

            foreach (var syntax in this.GetSyntaxNode())
                result.AppendLine(syntax.GetFullText());

            return String.Format("{0}", result);
        }

        # region • Methods - IEnumerable •

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> 
        /// that can be used to iterate through the collection.
        /// </returns>
        IEnumerator<String> IEnumerable<String>.GetEnumerator()
        {
            foreach (var line in this.Text.ToString().Split("\n".ToCharArray()))
                yield return line;
        }

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> 
        /// that can be used to iterate through the collection.
        /// </returns>
        IEnumerator<IEnumerable<String>> IEnumerable<IEnumerable<String>>.GetEnumerator()
        {
            var @this = this as IEnumerable<String>;

            yield return @this;
        }

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator"/> 
        /// object that can be used to iterate through the collection.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.Text.ToString().GetEnumerator();
        }

        # endregion Methods - IEnumerable

        # endregion Methods
    }

    public class Comments : IVisitable<Comments>
    {
        # region • Fields & Properties •

        /// <summary>
        /// Holds the comments.
        /// </summary>
        private readonly List<IBaseComment> commentList
            = new List<IBaseComment>();

        /// <summary>
        /// Gets the comments.
        /// </summary>
        public IEnumerable<IBaseComment> CommentList
        {
            get { return this.commentList; }
        }

        # endregion Fields & Properties

        # region • Methods •

        /// <summary>
        /// Gets the syntax node.
        /// </summary>
        /// <returns></returns>
        public SyntaxTriviaList GetSyntaxNode()
        {
            return Roslyn.Compilers.CSharp.Syntax.TriviaList(
                this.CommentList.SelectMany(x => x.GetSyntaxNode()));
        }

        /// <summary>
        /// Visitor pattern.
        /// </summary>
        /// <param name="visitor"></param>
        /// <returns></returns>
        public virtual Comments Accept(ISyntaxVisitor<Comments> visitor)
        {
            return visitor.Visit(this);
        }

        /// <summary>
        /// Adds the specified append line.
        /// </summary>
        /// <param name="code">The code.</param>
        public virtual Comments Add(String code)
        {
            if (String.IsNullOrEmpty(code))
                return this;

            return this;
        }

        /// <summary>
        /// Returns a <see cref="string"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="string"/> that represents this instance.
        /// </returns>
        public override String ToString()
        {
            var result = new StringBuilder();

            foreach (var syntax in this.GetSyntaxNode())
                result.AppendLine(syntax.GetFullText());

            return String.Format("{0}", result);
        }

        # endregion Methods

        # region • Types •

        public class Summary : Comment
        {
            # region • Fields & Properties •

            public override SyntaxTriviaList SyntaxTriviaList
            {
                get
                {
                    return Roslyn.Compilers.CSharp.Syntax.ParseLeadingTrivia(
                        String.Format(
@"/// <summary>
{0}
</summary>", this.Text));
                }
            }

            # endregion Fields & Properties
        }

        public class Value : Comment
        {
            # region • Fields & Properties •

            public override SyntaxTriviaList SyntaxTriviaList
            {
                get
                {
                    return Roslyn.Compilers.CSharp.Syntax.ParseLeadingTrivia(
                        String.Format("/// <value>{0}</value>", this.Text));
                }
            }

            # endregion Fields & Properties
        }

        # endregion Types
    }

    namespace MyNamespace
    {
        public abstract class Comment<TSelf, TSyntaxNode> : BaseSyntaxNode<TSelf, TSyntaxNode>
            where TSelf       : Comment<TSelf, TSyntaxNode>
            where TSyntaxNode : StructuredTriviaSyntax
        {
            # region • Fields & Properties •

            /// <summary>
            /// Holds the parent.
            /// </summary>
            private IBaseSyntaxNode parent;

            /// <summary>
            /// Gets the parent.
            /// </summary>
            public override IBaseSyntaxNode Parent
            {
                get { return this.parent; }
                internal set { this.parent = value; }
            }

            # endregion Fields & Properties

            # region • Ctor's •

            /// <summary>
            /// Initializes a new instance of the <see cref="Comment&lt;TSelf, TSyntaxNode&gt;"/> class.
            /// </summary>
            protected Comment() { }

            /// <summary>
            /// Initializes a new instance of the <see cref="Comment&lt;TSelf, TSyntaxNode&gt;"/> class.
            /// </summary>
            /// <param name="parent">The parent.</param>
            protected Comment(IBaseSyntaxNode parent) : this()
            {
                if (this.parent == null)
                    return;

                this.parent = parent;
            }

            # endregion Ctor's
        }
    }
}
